package com.yiren.dbaa.module.practice_set.site.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.bean.CompanyBean;
import com.yiren.dbaa.module.practice_set.site.contract.AddSiteRuleInfoContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/10/21 14:28
 * @description:  新建规则信息M
 */
public class AddSiteRuleInfoModel implements AddSiteRuleInfoContract.Model {
    private Activity activity;
    private Gson gson;

    public AddSiteRuleInfoModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }


    @Override
    public void getAllCompany(CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadAllCompany(), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<CompanyBean>> testBaseBean = gson.fromJson(result,new TypeToken<TestBaseBean<List<CompanyBean>>>() {}.getType());
                if (testBaseBean.isSuccess()){
                    callBack.success(testBaseBean.getData());
                }
            }
            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });


    }

    @Override
    public void getCitySiteName(String city,CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadExamByPid("?pid=" + city), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<SiteEntity>>  testBaseBean = gson.fromJson(result,new TypeToken<TestBaseBean<List<SiteEntity>>>() {}.getType());
                if (testBaseBean.isSuccess()){
                    callBack.success(testBaseBean.getData());
                }
            }
            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });


    }

}
